<?php

namespace app\order\logic;

use app\common\library\RedissLock;
use app\common\service\JinyiApiService;
use app\common\service\YiJTApiService;
use app\common\service\ShunfengApiService;
use app\data\util\MqQueueName;
use app\systems\logic\MqTaskPushLogic;
use think\facade\Db;
use think\facade\Cache;

class OrderShipServiceLogic
{
    /**
     * 订单发货队列消费
     * @param $data
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderShippingQueueConsume($data)
    {

        htmlLog(['data'=>$data],OPERATE_LOG . date('Ym') . '/orderShippingQueueConsume/' . date('Ymd') . '.txt');

        if(!$data['warehouse_id'] || !$data['order_id'])  return  true;
            $log =[];
            $log['code']         = 1;
            $log['order_id']     = $data['order_id'];
            $log['shop_id']      = $data['shop_id'];
            $log['time']         = time();
            $log['warehouse_id'] = $data['warehouse_id'];
            OrderShipLogic::insertOrderShipLog($log);
            $YiJTApi = new YiJTApiService();
            $YiJTApi->createOrder($data);

        return  true;
    }


    /**
     * @desc    金蚁自发货回传物流单号生产
     * @date    2024/4/13
     * @author  luckyXiong updateJinYiTrackingNumber
     */
    public function updateJinYiTrackingNumber($param=[]){
        return (new JinyiApiService())->update_tracking_number($param);
    }

    /**
     * @desc     易境通发货回传物流单号正常
     * @date     2024/12/07
     * @author   zhangheng
     */
    public function updateJYiJTTrackingNumber($param=[]){
        return (new YiJTApiService())->update_tracking_number($param);
    }


    /**
     * @desc     顺丰物流单号回传
     * @date     2024/7/12
     * @author   luckyXiong
     */
    public function sfTrackNumber($param=[]){
        htmlLog(['params'=>$param],OPERATE_LOG . date('Ym') . '/sfTrackNumbers/' . date('Ymd') . '.txt');
        $track_number = $param['track_number']??'';
        $order_id     = $param['order_id']??'';
        if(!$track_number)   return  '暂无物流单号!';
        if(!$order_id)       return  '暂无订单!';
        Db::name('order_shopify_list')->where('order_id',$order_id)->update(
            [
                'tracking_number'=>$track_number,
                'ship_status'    =>4,
                'update_time'    =>date("Y-m-d H:i:s", time()),
            ]
        );
        $shop_id = Db::name('order_shopify_list')->where('order_id',$order_id)->value('shop_id');
        //数据推送到队列处理,2024-07-17
        MqTaskPushLogic::mq_task_push(
            '',
            MqQueueName::QUEUE_TIK_TRACK_TO_SHOPIFY_QUEUE,
            [
                'order_id'       =>$order_id,
                'shop_id'        =>$shop_id,
                'tracking_number'=>$track_number,
            ],
            false
        );
    }


    /**
     * @desc     更新金蚁token
     * @date     2024/7/12
     * @author   luckyXiong
     */
    public function jinyiToken($param=[]){
        htmlLog(['data'=>$param],OPERATE_LOG . date('Ym') . '/jinyiToken/' . date('Ymd') . '.txt');
        $token = $param['token']??'';
        if(!$token)       return  '暂无token!';
        Db::name('jinyi_token')->where('id',1)->update(
            [
                'token'=>$token,
            ]
        );
    }




}
